// You have generated a new plugin project without specifying the `--platforms`
// flag. A plugin project with no platform support was generated. To add a
// platform, run `flutter create -t plugin --platforms <platforms> .` under the
// same directory. You can also find a detailed instruction on how to add
// platforms in the `pubspec.yaml` at
// https://flutter.dev/to/pubspec-plugin-platforms.

import 'flutter_ffmpeg_platform_interface.dart';

mixin class FlutterFfmpegMixin {
  static Future<String?> getPlatformVersion() {
    return FlutterFfmpegPlatform.instance.getPlatformVersion();
  }

  static List<String> parseArguments(String command) {
    final List<String> argumentList = List<String>.empty(growable: true);
    StringBuffer currentArgument = new StringBuffer();

    bool singleQuoteStarted = false;
    bool doubleQuoteStarted = false;

    for (int i = 0; i < command.length; i++) {
      int? previousChar;
      if (i > 0) {
        previousChar = command.codeUnitAt(i - 1);
      } else {
        previousChar = null;
      }
      final currentChar = command.codeUnitAt(i);

      if (currentChar == ' '.codeUnitAt(0)) {
        if (singleQuoteStarted || doubleQuoteStarted) {
          currentArgument.write(String.fromCharCode(currentChar));
        } else if (currentArgument.length > 0) {
          argumentList.add(currentArgument.toString());
          currentArgument = new StringBuffer();
        }
      } else if (currentChar == '\''.codeUnitAt(0) &&
          (previousChar == null || previousChar != '\\'.codeUnitAt(0))) {
        if (singleQuoteStarted) {
          singleQuoteStarted = false;
        } else if (doubleQuoteStarted) {
          currentArgument.write(String.fromCharCode(currentChar));
        } else {
          singleQuoteStarted = true;
        }
      } else if (currentChar == '\"'.codeUnitAt(0) &&
          (previousChar == null || previousChar != '\\'.codeUnitAt(0))) {
        if (doubleQuoteStarted) {
          doubleQuoteStarted = false;
        } else if (singleQuoteStarted) {
          currentArgument.write(String.fromCharCode(currentChar));
        } else {
          doubleQuoteStarted = true;
        }
      } else {
        currentArgument.write(String.fromCharCode(currentChar));
      }
    }

    if (currentArgument.length > 0) {
      argumentList.add(currentArgument.toString());
    }

    return argumentList;
  }
}

class FlutterFfmpeg with FlutterFfmpegMixin {
  static Future<String> getPlatformVersion() async {
    return FlutterFfmpegPlatform.instance.getPlatformVersion();
  }

  static Future<void> execute(String command) {
    return FlutterFfmpegPlatform.instance.ffmpegExecute(
        ["ffmpeg", ...FlutterFfmpegMixin.parseArguments(command)]);
  }

  static Future<void> executeWithArguments(List<String> command) {
    return FlutterFfmpegPlatform.instance.ffmpegExecute(["ffmpeg", ...command]);
  }
}

class FlutterFfprobe with FlutterFfmpegMixin {
  static Future<String> execute(String command) {
    return FlutterFfmpegPlatform.instance.ffprobeExecute(
        ["ffprobe", ...FlutterFfmpegMixin.parseArguments(command)]);
  }

  static Future<String> executeWithArguments(List<String> command) {
    return FlutterFfmpegPlatform.instance
        .ffprobeExecute(["ffprobe", ...command]);
  }
  
}
